<?php
use Carbon\Carbon;
class Admin_PromotionController extends BaseController
{
    protected $layout = 'layouts.admin';
    protected $resourceView = 'admin.promotion';
    protected $validatorMessages = array(
		  'promotion_name.required'      => '请输入活动名。',
		);
		
	public function index()
    {
		$datas = Input::all();
		$offset = Input::get('offset', 0);
		$limit = Input::get('limit', 1000000000);
		$datas = Promotion::getTrainPromotions(Input::get('search'), $offset, $limit, $order_by = 'promotion_start_time');
		$this->layout->content = View::make($this->resourceView.'.index')->with(compact('datas'));				
    }

	public function create()
    {
        $this->layout->content = View::make('admin.promotion.create');
	}  

	public function edit($id)
    {
		$promotion = Promotion::find($id);
        $this->layout->content = View::make('admin.promotion.edit')->with(compact('promotion'));
	}  

	public function store()
	{
		$datas = Input::all();

		//处理图片
		if( $img = Input::file('img'))
		{
			$upload_path = Config::get('app.upload_dir');
			$dir = '/promotions'.date("/Y/m/", time());
			$upload_result = UploadHelper::uploadImage('img', $upload_path.$dir);
            if ($upload_result['error'] == 0) {
            } else {
                throw new Exception('上传图片失败。');
            }
			$datas['img'] = $upload_result['files'][0]['path'];
		}
		Promotion::storeTrainPromotion($datas);
        return Redirect::route('admin.promotion.index');
	}

	public function update($id)
	{
		$datas = Input::all();
		$promotion = Promotion::find($id);
		//处理图片
		if( $img = Input::file('img'))
		{
			$upload_path = Config::get('app.upload_dir');
			$dir = '/promotions'.date("/Y/m/", time());
			$upload_result = UploadHelper::uploadImage('img', $upload_path.$dir);
            if ($upload_result['error'] == 0) {
            } else {
                throw new Exception('上传图片失败。');
            }
			$datas['img'] = $upload_result['files'][0]['path'];
		}
		Promotion::updateTrainPromotion($datas, $id);
        return Redirect::route('admin.promotion.index');
	}

	public function destroy($id)
	{
		$data = App::make('Promotion')->find($id);
		if (is_null($data))
		{
				return Redirect::back()->with('error', '没有找到对应的活动。');
		} else {
				if($data->img)File::delete($data->img);
				$data->delete();
				return Redirect::back()->with('success', '活动删除成功。');
		}
	}

	public function join($id)
    {
		$promotion = Promotion::find($id);
		$query = PromotionJoin::where('promotion_id', $id);
		$datas = $query->paginate(Config::get('app.page_row'));
        $this->layout->content = View::make('admin.promotion.join')->with(compact('promotion', 'datas'));
	}  

	public function editJoin($id)
    {
		$data = PromotionJoin::find($id);
		$promotion = Promotion::find($data->promotion_id);
        $this->layout->content = View::make('admin.promotion.editjoin')->with(compact('promotion', 'data'));
	}  

	public function updateJoin($id)
    {
		$data = PromotionJoin::find($id);
		$data->content = Input::get('content');
		$data->save();
		return Redirect::route('admin.join.index', $data->promotion_id);
	}  
}
